package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolDemo2 {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        for(int i = 0 ; i < 5 ; i ++){
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread t = Thread.currentThread();
                        System.out.println("当前线程被执行："+t.getName());
                        Thread.sleep(5000);
                        System.out.println("当前线程执行完毕");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
            threadPool.execute(r);
            System.out.println("有一个线程加入了线程池");
        }

        threadPool.shutdown();
        //threadPool.shutdownNow();
        System.out.println("关闭线程池");
    }
}
